
package com.oahcfly.chgame.core.ui;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.oahcfly.chgame.core.freefont.FreeFont;
import com.oahcfly.chgame.core.freefont.FreeLabel;
import com.oahcfly.chgame.core.helper.CHPixmapHelper;
import com.oahcfly.chgame.core.mvc.CHActor;

/**
 * 跑马灯
 * @author haocao
 *
 */
public class CHMarqueeText extends CHActor {

	// 文本Label
	private FreeLabel label;
	private Texture bgTexture;

	public void setMarqueeTextParams (String text, Color textColor, int width, int moveSpeed) {
		stoped = false;
		label = FreeFont.getLabel(textColor, text);
		// 设置透明背景
		Color alphaColor = new Color(Color.BLACK);
		alphaColor.a = 0.2f;
		bgTexture = CHPixmapHelper.createRectangleTexture(alphaColor, width, 20 + FreeFont.getBitmapFont().getSize());
		setBgTexture(bgTexture);
		addClipRectangle(new Rectangle(0, 0, width, getHeight()));

		// float moveDistance = Math.max(0, label.getWidth() - width);
		float duration = Math.max(0, 12 - 3 * moveSpeed / 100f);
		if (label.getWidth() > width) {
			label.addAction(Actions.repeat(-1,
				Actions.sequence(Actions.moveBy(-label.getWidth(), 0, duration), Actions.run(new Runnable() {

					@Override
					public void run () {
						// 从尾部重新开始
						label.setX(getRight());
					}
				}), Actions.moveBy(-label.getWidth() - width, 0, duration))));
		}
	}

	@Override
	public void drawAfterBg (Batch batch) {
		if (label.getX() == 0) {
			// 设置初始坐标
			label.setX(getX());
		}
		label.draw(batch, 1);
		// 随着action的执行，坐标会发生变化，在这里进行赋值更新
		label.setPosition(label.getX(), getY() + 5f);
	}

	@Override
	public void act (float delta) {
		if (!stoped) {
			label.act(delta);
		}
		super.act(delta);
	}

	public FreeLabel getLabel () {
		return label;
	}

	@Override
	public boolean remove () {
		if (bgTexture != null) {
			bgTexture.dispose();
		}
		return super.remove();
	}

	private boolean stoped;

	public void stopMarquee () {
		stoped = true;
	}

	public void resumeMarquee () {
		stoped = false;
	}

	public static class Builder {
		private String text;
		private Color textColor;
		private int marqueeWidth;
		private int moveSpeed = 50;

		public Builder setText (String text) {
			this.text = text;
			return this;
		}

		public Builder setTextColor (Color textColor) {
			this.textColor = textColor;
			return this;
		}

		public Builder setMarqueeWidth (int marqueeWidth) {
			this.marqueeWidth = marqueeWidth;
			return this;
		}

		/**
		 * 范围【0~100】越大速度越快
		 * @param moveSpeed
		 * @return 
		 */
		public Builder setMoveSpeed (int moveSpeed) {
			this.moveSpeed = moveSpeed > 100 ? 100 : moveSpeed < 0 ? 0 : moveSpeed;
			return this;
		}

		public CHMarqueeText build () {
			CHMarqueeText chMarqueeText = CHActor.obtain(CHMarqueeText.class);
			chMarqueeText.setMarqueeTextParams(text, textColor, marqueeWidth, moveSpeed);
			return chMarqueeText;
		}
	}

}
